package com.sooth.system.menu.service;

import cn.hutool.core.collection.CollUtil;
import com.dream.boot.impl.ServiceImpl;
import com.sooth.common.satoken.utils.LoginHelper;
import com.sooth.system.menu.mapper.SysMenuMapper;
import com.sooth.system.menu.view.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

/**
 * 菜单 业务层处理
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuVo, SysMenuBo> implements ISysMenuService {

    private final SysMenuMapper menuMapper;

    /**
     * 查询系统菜单列表
     *
     * @param menu 菜单信息
     * @return 菜单列表
     */
    @Override
    public List<SysMenuVo> selectMenuTree(SysMenuDto menu) {
        List<SysMenuVo> menuList;
        // 管理员显示所有菜单信息
        if (LoginHelper.isSuperAdmin()) {
            menuList = templateMapper.selectTree(SysMenuVo.class, menu);
        } else {
            menuList = menuMapper.selectMenuTree(LoginHelper.getUserId(), menu);
        }
        return menuList;
    }

    @Override
    public List<RouterView> selectRouterTree() {
        SysMenuDto menuModel = new SysMenuDto();
        menuModel.setMenuType(List.of(0, 1));
        return buildMenus(selectMenuTree(menuModel));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateWithOrderById(SysMenuBo menu) {
        Long parentId = menu.getParentId();
        Integer orderNum = menu.getOrderNum();
        List<Long> menuIds = menuMapper.queryByOrderNum(parentId, orderNum);
        if (!CollUtil.isEmpty(menuIds) && !menuIds.contains(menu.getId())) {
            menuMapper.updateOrderInc(parentId, orderNum);
        }
        return super.updateById(menu);
    }

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public List<String> queryPermByUserId(Long userId) {
        return menuMapper.queryPermByUserId(userId);
    }

    /**
     * 构建前端路由所需要的菜单
     *
     * @param menus 菜单列表
     * @return 路由列表
     */
    public List<RouterView> buildMenus(List<SysMenuVo> menus) {
        List<RouterView> routers = new LinkedList<>();
        for (SysMenuVo menu : menus) {
            MetaView metaView = new MetaView();
            metaView.setTitle(menu.getMenuName());
            metaView.setIcon(menu.getIcon());
            metaView.setIsKeepAlive(1 == menu.getKeepAlive());
            metaView.setIsHide(1 == menu.getHide());
            metaView.setPerm(menu.getPerm());
            metaView.setParam(menu.getParam());
            metaView.setIsAffix(1 == menu.getAffix());
            RouterView router = new RouterView();
            String componentName = menu.getComponentName();
            String component = menu.getComponent();
            Integer componentType = menu.getComponentType();
            if (componentType == 1) {
                metaView.setIsFull(true);
            } else if (componentType == 2) {
                metaView.setIsLink(true);
            }
            router.setName(componentName);
            router.setPath(menu.getPath());
            router.setComponent(component);
            router.setParam(menu.getParam());
            router.setMeta(metaView);
            List<SysMenuVo> cMenus = menu.getChildren();
            if (CollUtil.isNotEmpty(cMenus)) {
                if (0 == menu.getMenuType()) {
                    router.setRedirect(cMenus.get(0).getPath());
                }
                router.setChildren(buildMenus(cMenus));
            }
            routers.add(router);
        }
        return routers;
    }
}
